package com.zjdiepu.www.biz.third;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.third.BaseToMessageMapper;
import com.zjdiepu.www.ibiz.third.IBaseToMessageService;
import com.zjdiepu.www.model.third.dto.BaseToMessage;
import com.zjdiepu.www.model.third.ext.BaseToMessageExt;

/**
 * Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年04月13日 16时15分<br/>
 * @since 1.0<br/>
 */
@Service
public class BaseToMessageServiceImpl extends BaseService implements IBaseToMessageService {
	public static final Logger logger = LoggerFactory.getLogger(BaseToMessageServiceImpl.class);

	@Autowired
	private BaseToMessageMapper baseToMessageMapper;
	
	@Override
	public void saveUpdateAndDelete(BaseToMessage baseToMessage, OperateType operType) {
		switch (operType) {
		case add:
			baseToMessage.initInsertData(baseToMessage.getModifier(), baseToMessage.getModifierName());
			baseToMessageMapper.insert(baseToMessage);
			break;
		case edit:
			baseToMessage.initUpdateData(baseToMessage.getModifier(), baseToMessage.getModifierName());
			// 影响的记录数
			int updateCount = baseToMessageMapper.updateByPrimaryKeySelective(baseToMessage);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			baseToMessage.initDeleteData(baseToMessage.getModifier(), baseToMessage.getModifierName());
			// 影响的记录数
			int deleteCount = baseToMessageMapper.updateByPrimaryKeySelective(baseToMessage);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<BaseToMessage> selectByExt(BaseToMessageExt baseToMessageExt) {
		return baseToMessageMapper.selectByExt(baseToMessageExt);
	}
	
	@Override
	public int countByExt(BaseToMessageExt baseToMessageExt){
		return baseToMessageMapper.countByExt(baseToMessageExt);
	}
	
	@Override
	public BaseToMessage selectById(String id) {
		return baseToMessageMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			baseToMessageMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(BaseToMessage baseToMessage) {
		return baseToMessageMapper.countRefined(baseToMessage);
	}

	@Override
	public BaseToMessage selectRefined(BaseToMessage baseToMessage) {
		return baseToMessageMapper.selectRefined(baseToMessage);
	}

	@Override
	public boolean repeat(BaseToMessage baseToMessage) {
		int count = countRefined(baseToMessage);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(baseToMessage.getId())) {
			return true;
		}
		BaseToMessage dbItem = selectRefined(baseToMessage);
		if(null == dbItem) {
			return false;
		}
		return !baseToMessage.getId().equals(dbItem.getId());
	}
}
